package com.chaoyi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.ModelInfo;
import com.chaoyi.service.ModelInfoService;

@Controller
@RequestMapping("/modelInfo")
public class ModelInfoController {
	
	@Resource
	private ModelInfoService modelInfoService;
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mod = new ModelAndView("/modelInfo/modelInfoList");
		return mod;
	}

	/**
	 * 添加父菜单数据页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/add_plus")
	public ModelAndView add_plus(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("/modelInfo/modelInfo_add");
		return mv;
	}
	
	/**
	 * 修改父菜单数据页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/edit_plus")
	public ModelAndView edit_plus(HttpServletRequest request, HttpServletResponse response,int modelId) {
		ModelInfo modelInfo = modelInfoService.findModel(modelId);
		ModelAndView mv = new ModelAndView("/modelInfo/modelInfo_edit");
		mv.addObject("obj",modelInfo);
		return mv;
	}
	

	/**
	 * 跳转新增子页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/addChild_plus")
	public ModelAndView addChild_plus(HttpServletRequest request, HttpServletResponse response, int parentId) {
		ModelAndView mv = new ModelAndView("/modelInfo/modelInfoChild_add");
		mv.addObject("obj", parentId);
		return mv;
	}

	/**
	 * 父列表展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index_json")
	@ResponseBody
	public JSONObject index_json(HttpServletRequest request, HttpServletResponse response) {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<ModelInfo> modelInfoList = modelInfoService.findList(offset, limit);
		JSONObject json = new JSONObject();
		json.put("rows", modelInfoList);
		json.put("total", modelInfoList.size());
		return json;
	}
	
	/**
	 * 子列表展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/children_json")
	@ResponseBody
	public JSONObject children_json(HttpServletRequest request, HttpServletResponse response,int parentId) {
		List<ModelInfo> modelInfoList = modelInfoService.findChildrenList(parentId);
		JSONObject json = new JSONObject();
		json.put("rows", modelInfoList);
		json.put("total", modelInfoList.size());
		return json;
	}

	/**
	 * 保存新增菜单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public JSONObject add(HttpServletRequest request, HttpServletResponse response, ModelInfo modelInfo) {
		JSONObject json = new JSONObject();
		int number = modelInfoService.saveModelInfoParent(modelInfo);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	/**
	 * 保存修改菜单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("edit")
	public JSONObject edit(HttpServletRequest request, HttpServletResponse response, ModelInfo modelInfo) {
		JSONObject json = new JSONObject();
		int number = modelInfoService.updateModelInfoParent(modelInfo);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 验证名称是否重复
	 * 
	 * @param modelInfoName
	 * @param id
	 * @return JSONObject
	 */
	@ResponseBody
	@RequestMapping("checkModelInfoName")
	public JSONObject checkModelInfoName(HttpServletRequest request, HttpServletResponse response,
			String modelName, int modelId) {
		JSONObject json = new JSONObject();
		int number = modelInfoService.checkModelInfoName(modelName, modelId);
		if (number != 0) {
			json.put("valid", false);
			return json;
		}
		json.put("valid", true);
		return json;
	}

}
